package com.yan.programercode.chapter_1.question_4;

import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {

	private Queue<PetEnterQueue> dogQ;
	private Queue<PetEnterQueue> catQ;
	private long count;
	
	public DogCatQueue(){
		dogQ = new LinkedList<PetEnterQueue>();
		catQ = new LinkedList<PetEnterQueue>();
		this.count = 0;
	}
	
	public void add(Pet pet){
		if("dog".equals(pet.getPetType())){
			this.dogQ.add(new PetEnterQueue(pet, this.count++));
		}else if("cat".equals(pet.getPetType())){
			this.catQ.add(new PetEnterQueue(pet, this.count++));
		}else{
			throw new RuntimeException("err, not dog or cat.");
		}
	}
	
	public Pet pollAll(){
		if(!this.dogQ.isEmpty() && !this.catQ.isEmpty()){
			if(this.dogQ.peek().getCount() < this.catQ.peek().getCount()){
				return this.dogQ.poll().getPet();
			}else{
				return this.catQ.poll().getPet();
			}
		}else if(!this.dogQ.isEmpty()){
			return this.dogQ.poll().getPet();
		}else if(!this.catQ.isEmpty()){
			return this.catQ.poll().getPet();
		}else{
			throw new RuntimeException("err, queue is empty.");
		}
	}
	
	public Dog pollDog(){
		if(!this.dogQ.isEmpty()){
			return (Dog)this.dogQ.poll().getPet();
		}else{
			throw new RuntimeException("err, dog queue is empty.");
		}
	}
	
	public Cat pollCat(){
		if(!this.catQ.isEmpty()){
			return (Cat)this.catQ.poll().getPet();
		}else{
			throw new RuntimeException("err, cat queue is empty.");
		}
	}
	
	public boolean isEmpty(){
		return this.dogQ.isEmpty() && this.catQ.isEmpty();
	}
	
	public boolean isDogEmpty(){
		return this.dogQ.isEmpty();
	}
	
	public boolean isCatEmpty(){
		return this.catQ.isEmpty();
	}
	
}
